package com.example.restatr.Atcivity.KitchenHelper;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.KitchenHelper.Bean.DishDetailsBean;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DetailsOfDishesAt extends AppCompatActivity {
    private ImageView img;
    private ImageView back;
    private ImageView head;
    private TextView title1;
    private TextView title2;
    private TextView title3;
    private LinearLayout ll1;
    private LinearLayout ll2;
    private ImageView icon1;
    private TextView num1;
    private ImageView icon2;
    private TextView num2;
    private RecyclerView rv;
    private List<DishDetailsBean.DataBean.StepListBean> stepList = new ArrayList<>();
    private DishDetailsBean.DataBean data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_details_of_dishes_at );
        img = findViewById( R.id.img );
        back = findViewById( R.id.back );
        head = findViewById( R.id.head );
        ll1 = findViewById( R.id.ll1 );
        ll2 = findViewById( R.id.ll2 );
        title1 = findViewById( R.id.title1 );
        title2 = findViewById( R.id.title2 );
        title3 = findViewById( R.id.title3 );
        icon1 = findViewById( R.id.icon1 );
        num1 = findViewById( R.id.num1 );
        icon2 = findViewById( R.id.icon2 );
        num2 = findViewById( R.id.num2 );
        rv = findViewById( R.id.rv );
        GetDishDetails();
        //点赞/取消点赞某个菜品
        ll1.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread( () -> {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put( "dishesId", data.getId() );
                        jsonObject.put( "newState", !data.isMyLikeState() );
                        String token = Tools.Post( "/prod-api/api/kitchen-helper/dishes-like",
                                MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                        JSONObject jx = new JSONObject( token );
                        String msg = jx.getString( "msg" );
                        int code = jx.getInt( "code" );
                        runOnUiThread( () -> {
                            Toast.makeText( DetailsOfDishesAt.this, msg, Toast.LENGTH_SHORT ).show();
                            if (code == 200) {
                                data.setMyLikeState( !data.isMyLikeState() );
                                if (data.isMyLikeState()) {
                                    data.setLikeCount( data.getLikeCount() + 1 );
                                } else {
                                    data.setLikeCount( data.getLikeCount() - 1 );
                                }
                                SetState();
                            }
                        } );
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } ).start();

            }
        } );
        //收藏/取消收藏某个菜品
        ll2.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread( () -> {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put( "dishesId", data.getId() );
                        jsonObject.put( "newState", !data.isMyCollectionState() );
                        String token = Tools.Post( "/prod-api/api/kitchen-helper/dishes-collection",
                                MainActivity.sp.getString( "token", "" ), jsonObject.toString() );
                        JSONObject jx = new JSONObject( token );
                        String msg = jx.getString( "msg" );
                        int code = jx.getInt( "code" );
                        runOnUiThread( () -> {
                            Toast.makeText( DetailsOfDishesAt.this, msg, Toast.LENGTH_SHORT ).show();
                            if (code == 200) {
                                data.setMyCollectionState( !data.isMyCollectionState() );
                                if (data.isMyCollectionState()) {
                                    data.setCollectionCount( data.getCollectionCount() + 1 );
                                } else {
                                    data.setCollectionCount( data.getCollectionCount() - 1 );
                                }
                                SetState();
                            }
                        } );
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } ).start();
            }
        } );
        back.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        } );
    }

    private void SetState() {
        if (data.isMyLikeState()) {
            icon1.setImageResource( R.drawable.ic_baseline_favorite_24 );
        } else {
            icon1.setImageResource( R.drawable.ic_baseline_favorite_border_24 );
        }
        if (data.isMyCollectionState()) {
            icon2.setImageResource( R.drawable.ic_round_star_24 );
        } else {
            icon2.setImageResource( R.drawable.ic_round_star_border_24 );
        }
        num1.setText( String.valueOf( data.getLikeCount() ) );
        num2.setText( String.valueOf( data.getCollectionCount() ) );
    }

    private void GetDishDetails() {
        new Thread( () -> {
            String get = Tools.Get( "/prod-api/api/kitchen-helper/dishes/" + getIntent().getIntExtra( "id", 0 ),
                    MainActivity.sp.getString( "token", "" ) );
            DishDetailsBean dishDetailsBean = new Gson().fromJson( get, DishDetailsBean.class );
            data = dishDetailsBean.getData();
            stepList = data.getStepList();
            DishDetailsBean.DataBean.AuthorBean author = data.getAuthor();
            runOnUiThread( () -> {
                Glide.with( DetailsOfDishesAt.this )
                        .load( Tools.ip + data.getImgUrl() )
                        .into( img );
                Glide.with( DetailsOfDishesAt.this )
                        .load( Tools.ip + author.getAvatar() )
                        .transform( new CircleCrop() )
                        .into( head );
                title1.setText( data.getName() );
                title2.setText( author.getName() );
                title3.setText( data.getIngredients() );
                SetState();
                rv.setAdapter( new adapter() );
            } );

        } ).start();
    }

    class adapter extends Adapter<adapter.viewHolder> {
        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.img_layout, parent, false );
            return new viewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.text1.setText( "步骤" + (position + 1) );
            holder.text2.setText( stepList.get( position ).getContent() );
            Glide.with( DetailsOfDishesAt.this )
                    .load( Tools.ip + stepList.get( position ).getImgUrl() )
                    .transform( new RoundedCorners( 15 ) )
                    .into( holder.img );
        }

        @Override
        public int getItemCount() {
            return stepList.size();
        }

        class viewHolder extends ViewHolder {
            private TextView text1;
            private TextView text2;
            private ImageView img;

            public viewHolder(@NonNull View itemView) {
                super( itemView );
                text1 = itemView.findViewById( R.id.text1 );
                text2 = itemView.findViewById( R.id.text2 );
                img = itemView.findViewById( R.id.img );
            }
        }
    }

}